package com.sto.stoapp.widget;

import android.app.Activity;
import android.content.Context;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.lang.reflect.Method;

/**
 * 限制编辑框弹出虚拟键盘
 * 
 * @author Administrator
 * 
 */
public class StoEditText extends EditText {

	private Context mContext;
	private InputMethodManager imm;

	public StoEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		init();
	}

	public StoEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init();
	}

	public StoEditText(Context context) {
		super(context);
		mContext = context;
		init();
	}

	private void init() {
		//closeBoard();
		this.setSelectAllOnFocus(true);
		imm = (InputMethodManager) getContext().getSystemService(
				Context.INPUT_METHOD_SERVICE);
	}

	protected void closeBoard() {
		if (android.os.Build.VERSION.SDK_INT <= 10) {// 4.0以下
			this.setInputType(InputType.TYPE_NULL);
		} else {
			((Activity) mContext).getWindow().setSoftInputMode(
					WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
			try {
				Class<EditText> cls = EditText.class;
				Method setShowSoftInputOnFocus;
				setShowSoftInputOnFocus = cls.getMethod(
						"setShowSoftInputOnFocus", boolean.class);
				setShowSoftInputOnFocus.setAccessible(true);
				setShowSoftInputOnFocus.invoke(this, false);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public boolean isOpenIme() {
		return imm.isActive();
	}
}
